package com.zk.nakedfowl.barcodeMachineGrantManager.controller;

import com.zk.nakedfowl.common.entity.PageResult;
import com.zk.nakedfowl.common.entity.Result;
import com.zk.nakedfowl.common.entity.StatusCode;
import com.zk.nakedfowl.barcodeMachineGrantManager.pojo.MachineGrant;
import com.zk.nakedfowl.barcodeMachineGrantManager.service.MachineGrantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 控制器层
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/machineGrant")
public class MachineGrantController {

    @Autowired
    private MachineGrantService machineGrantService;


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(String role) {
        if(role.equals("admin")){
            return new Result(true, StatusCode.OK, "查询成功", machineGrantService.findAll());
        }else {
            return new Result(false,StatusCode.FAIL,"没有权限");
        }
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", machineGrantService.findById(id));
    }

    /**
     * 根据屠宰场ID查询
     * @param createBy
     * @return
     */
    @RequestMapping(value = "/find/{createBy}" , method = RequestMethod.GET)
    public Result findByCreateBy(@PathVariable String createBy, String role){
        if(role.equals("butcher")|| role.equals("admin")){
            return new Result(true, StatusCode.OK, "查询成功", machineGrantService.findByCreateBy(createBy));
        }else {
            return new Result(false,StatusCode.FAIL,"没有权限");
        }
    }


    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<MachineGrant> pageList = machineGrantService.findSearch(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<MachineGrant>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(true, StatusCode.OK, "查询成功", machineGrantService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param machineGrant
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody MachineGrant machineGrant) {
        machineGrantService.add(machineGrant);
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param machineGrant
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody MachineGrant machineGrant, @PathVariable String id) {
        machineGrant.setId(id);
        machineGrantService.update(machineGrant);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        machineGrantService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

}
